﻿using System.Globalization;
using University.Workshop.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using University.Workshop.Model;
using University.Workshop.Business;
using System.Collections.ObjectModel;

namespace University.Workshop.Tests
{
    
    
    /// <summary>
    ///This is a test class for TicketDataAccessTest and is intended
    ///to contain all TicketDataAccessTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TicketDataAccessTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetProductByTicketId
        ///</summary>
        [TestMethod()]
        public void GetProductByTicketIdTest()
        {
            string firstName = "Pedro"; // TODO: Initialize to an appropriate value
            string lastName = "Suarez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "pedro" + rand.Next(400) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product expected = ProductRegistrationManager.RegisterNewProduct("computadoras portatiles", "512", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Bolivianos", buyer.Id, "1");
            products.Add(expected);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "SomeIssue", Description = "SomeDescription", OrderId = orderId });
            long ticketId = ticket.Id; // TODO: Initialize to an appropriate value
             // TODO: Initialize to an appropriate value
            Product actual;
            actual = TicketDataAccess.GetProductByTicketId(ticketId);
            Assert.AreEqual(expected.Name, "computadoras portatiles");
        }

        /// <summary>
        ///A test for GetTicketById
        ///</summary>
        [TestMethod()]
        public void GetTicketByIdTest()
        {
            string firstName = "Pedro"; // TODO: Initialize to an appropriate value
            string lastName = "Suarez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "pedro" + rand.Next(400) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("computadoras portatiles", "5234", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Bolivianos", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "SomeIssue", Description = "SomeDescription", OrderId = 1 });
            long ticketId = ticket.Id; // TODO: Initialize to an appropriate value
            Ticket expected = ticket; // TODO: Initialize to an appropriate value
            Ticket actual;
            actual = TicketDataAccess.GetTicketById(ticketId);
            Assert.AreEqual(expected.IssueTitle, actual.IssueTitle);
        }

        /// <summary>
        ///A test for RegisterTicket
        ///</summary>
        [TestMethod()]
        public void RegisterTicketTest()
        {
            Random rand = new Random();
            string firstName = "Pedro";// TODO: Initialize to an appropriate value
            string lastName = "Suarez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "pedroy" + rand.Next(400) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("laptops en buen estado", rand.Next(0,1000).ToString(CultureInfo.CurrentCulture), "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "SomeIssue", Description = "SomeDescription", OrderId = 1 });
            Assert.AreEqual(ticket.IssueTitle, "SomeIssue");
        }

        /// <summary>
        ///A test for ChangeTicketStatus
        ///</summary>
        [TestMethod()]
        public void ChangeTicketStatusTest()
        {
            string firstName = "Ariel"; // TODO: Initialize to an appropriate value
            string lastName = "Perez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "arielin" + rand.Next(1000) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Compu linda", "754", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            Ticket expected = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "new Ticket", Description = "this is a new ticket for test", OrderId = 2 });
            long ticketId = expected.Id; // TODO: Initialize to an appropriate value
            TicketStatus status = TicketStatus.InProcess; // TODO: Initialize to an appropriate value
            // TODO: Initialize to an appropriate value
            Ticket actual;
            actual = TicketDataAccess.ChangeTicketStatus(ticketId, status);
            Assert.AreEqual(expected.Id, actual.Id);
        }

        /// <summary>
        ///A test for GatherActiveTickets
        ///</summary>
        [TestMethod()]
        public void GatherActiveTicketsTest()
        {
            RegisterTicketTest();
            Collection<Ticket> actual = TicketDataAccess.GatherActiveTickets();
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for GetAllTicketReplies
        ///</summary>
        [TestMethod()]
        public void GetAllTicketRepliesTest()
        {
            string firstName = "Fred"; // TODO: Initialize to an appropriate value
            string lastName = "Perez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "fred" + rand.Next(1000) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Compu vieja", "15", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "other Ticket", Description = "this is another ticket for test", OrderId = 2 });
            long ticketId = ticket.Id; // TODO: Initialize to an appropriate value
            User user = UserDataAccess.GetUserByEmail(email);
            TicketReply ticketReply = new TicketReply {TicketId = ticketId, UserId = user.Id,Date = DateTime.Now, Reply = "I think the same thing"};
            TicketDataAccess.RegisterTicketReply(ticketReply);
            Collection<TicketReply> actual;
            actual = TicketDataAccess.GetAllTicketReplies(ticketId);
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for GetAllTicketsByBuyerId
        ///</summary>
        [TestMethod()]
        public void GetAllTicketsByBuyerIdTest()
        {
            string firstName = "Frankie"; // TODO: Initialize to an appropriate value
            string lastName = "Word"; // TODO: Initialize to an appropriate value
            char gender = 'F'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "frankie" + rand.Next(1000) + "@email.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "La Paz"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Samsumg galaxy nexus", "400", "Good Cellphone Good Cellphone Good Cellphone Good Cellphone Good Cellphone Good Cellphone ", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            var orders = OrderDataAccess.GetOrdersByBuyerId(buyer.Id);
            long idOfOrder = 1;
            foreach (Order order in orders)
            {
                if (order.ProductId == prod.ProductId)
                {
                    idOfOrder = order.Id;
                    break;
                }
            }
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "other Ticket", Description = "this is another ticket for test", OrderId = idOfOrder });
            Collection<Ticket> actual;
            actual = TicketDataAccess.GetAllTicketsByBuyerId(buyer.Id);
            Assert.IsNotNull(actual);
        }


        /// <summary>
        ///A test for GetTicketByOrderId
        ///</summary>
        [TestMethod()]
        public void GetTicketByOrderIdTest()
        {
            string firstName = "Fanny"; // TODO: Initialize to an appropriate value
            string lastName = "Word"; // TODO: Initialize to an appropriate value
            char gender = 'F'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "Fanny" + rand.Next(1000) + "@email.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "La Paz"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Sony Ericsson k700i", "90", "Good Cellphone Good Cellphone Good Cellphone Good Cellphone Good Cellphone Good Cellphone ", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            var orders = OrderDataAccess.GetOrdersByBuyerId(buyer.Id);
            long idOfOrder = 1;
            foreach (Order order in orders)
            {
                if (order.ProductId == prod.ProductId)
                {
                    idOfOrder = order.Id;
                    break;
                }
            }
            Ticket expected = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "other Ticket", Description = "this is another ticket for test", OrderId = idOfOrder });
            Ticket actual;
            actual = TicketDataAccess.GetTicketByOrderId(idOfOrder);
            Assert.AreEqual(expected.Id, actual.Id);
        }

    }
}
